home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / netscape / ntscp201.486 / ntscp201 / movemail-src / movemail.c < prev    next >
C/C++ Source or Header  |  1995-12-13  |  18KB  |  845 lines

  1. /* movemail foo bar -- move file foo to file bar,
  2.    locking file foo the way /bin/mail respects.
  3.    Copyright (C) 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Emacs.
  6.  
  7. GNU Emacs is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU Emacs is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU Emacs; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Extracted from XEmacs 19.13, which said: "synched up with: FSF 19.28." */
  22.  
  23. /* Important notice: defining MAIL_USE_FLOCK or MAIL_USE_LOCKF *will
  24.    cause loss of mail* if you do it on a system that does not normally
  25.    use flock as its way of interlocking access to inbox files.  The
  26.    setting of MAIL_USE_FLOCK and MAIL_USE_LOCKF *must agree* with the
  27.    system's own conventions.  It is not a choice that is up to you.
  28.  
  29.    So, if your system uses lock files rather than flock, then the only way
  30.    you can get proper operation is to enable movemail to write lockfiles there.
  31.    This means you must either give that directory access modes
  32.    that permit everyone to write lockfiles in it, or you must make movemail
  33.    a setuid or setgid program.  */
  34.  
  35. /*
  36.  * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
  37.  *
  38.  * Added POP (Post Office Protocol) service.  When compiled -DPOP
  39.  * movemail will accept input filename arguments of the form
  40.  * "po:username".  This will cause movemail to open a connection to
  41.  * a pop server running on $MAILHOST (environment variable).  Movemail
  42.  * must be setuid to root in order to work with POP.
  43.  * 
  44.  * New module: popmail.c
  45.  * Modified routines:
  46.  *    main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
  47.  *        after POP code. 
  48.  * New routines in movemail.c:
  49.  *    get_errmsg - return pointer to system error message
  50.  *
  51.  */
  52.  
  53. #define NO_SHORTNAMES   /* Tell config not to load remap.h */
  54. #ifndef NETSCAPE
  55. #include <../src/config.h>
  56. #endif /* !NETSCAPE */
  57. #include <stdlib.h>
  58. #include <unistd.h>
  59. #include <time.h> /* for time() */
  60. #include <stdio.h> /* for printf() */
  61. #include <string.h> /* strcpy() */
  62.  
  63. #include <sys/types.h>
  64. #include <sys/stat.h>
  65. #include <sys/file.h>
  66. #include <errno.h>
  67. #ifndef NETSCAPE
  68. #include <../src/syswait.h>
  69. #endif /* !NETSCAPE */
  70.  
  71. #ifdef NETSCAPE
  72.  
  73. #ifndef WAITTYPE
  74. #define WAITTYPE int
  75. #endif /* !WAITTYPE */
  76.  
  77. #ifndef WIFEXITED
  78. #include <sys/wait.h>
  79. #endif /* !WIFEXITED */
  80.  
  81. #ifndef WRETCODE
  82. #define WRETCODE(w) WEXITSTATUS(w)
  83. #endif /* !WRETCODE */
  84.  
  85. #endif /* NETSCAPE */
  86.  
  87. #ifdef MSDOS
  88. #undef access
  89. #endif /* MSDOS */
  90.  
  91. #ifdef USG
  92. #include <fcntl.h>
  93. #include <unistd.h>
  94. #if defined (sun)
  95. #include <stdlib.h>
  96. #endif /* sun */
  97. #ifndef F_OK
  98. #define F_OK 0
  99. #define X_OK 1
  100. #define W_OK 2
  101. #define R_OK 4
  102. #endif
  103. #endif /* USG */
  104.  
  105. #ifdef HAVE_UNISTD_H
  106. #include <unistd.h>
  107. #endif
  108.  
  109. #ifdef XENIX
  110. #include <sys/locking.h>
  111. #endif
  112.  
  113. #ifdef MAIL_USE_LOCKF
  114. #define MAIL_USE_SYSTEM_LOCK
  115. #endif
  116.  
  117. #ifdef MAIL_USE_FLOCK
  118. #define MAIL_USE_SYSTEM_LOCK
  119. #endif
  120.  
  121. #ifdef MAIL_USE_MMDF
  122. extern int lk_open (), lk_close ();
  123. #endif
  124.  
  125. /* Cancel substitutions made by config.h for Emacs.  */
  126. #undef open
  127. #undef read
  128. #undef write
  129. #undef close
  130.  
  131. static char *concat (char *s1, char *s2, char *s3);
  132. static void *xmalloc (unsigned int size);
  133. #ifndef errno
  134. extern int errno;
  135. #endif
  136.  
  137. static void error (char *s1, char *s2, char *s3);
  138. static void fatal (char *s1, char *s2);
  139. static void pfatal_with_name (char *name);
  140. static void pfatal_and_delete (char *name);
  141.  
  142. #ifndef HAVE_STRERROR
  143. char *strerror (int);
  144. #endif
  145.  
  146. /* Nonzero means this is name of a lock file to delete on fatal error.  */
  147. char *delete_lockname;
  148.  
  149. void
  150. main (argc, argv)
  151.      int argc;
  152.      char **argv;
  153. {
  154.   char *inname, *outname;
  155.   int indesc, outdesc;
  156.   int nread;
  157.   WAITTYPE status;
  158.  
  159. #ifndef MAIL_USE_SYSTEM_LOCK
  160.   struct stat st;
  161.   long now;
  162.   int tem;
  163.   char *lockname, *p;
  164.   char *tempname;
  165.   int desc;
  166. #endif /* not MAIL_USE_SYSTEM_LOCK */
  167.  
  168.   delete_lockname = 0;
  169.  
  170.   if (argc < 3)
  171.     fatal ("two arguments required", "");
  172.  
  173.   inname = argv[1];
  174.   outname = argv[2];
  175.  
  176. #ifdef MAIL_USE_MMDF
  177.   mmdf_init (argv[0]);
  178. #endif
  179.  
  180.   /* Check access to output file.  */
  181.   if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
  182.     pfatal_with_name (outname);
  183.  
  184.   /* Also check that outname's directory is writeable to the real uid.  */
  185.   {
  186.     char *buf = (char *) xmalloc (strlen (outname) + 1);
  187.     char *p;
  188.     strcpy (buf, outname);
  189.     p = buf + strlen (buf);
  190.     while (p > buf && p[-1] != '/')
  191.       *--p = 0;
  192.     if (p == buf)
  193.       *p++ = '.';
  194.     if (access (buf, W_OK) != 0)
  195.       pfatal_with_name (buf);
  196.     free (buf);
  197.   }
  198.  
  199. #ifdef MAIL_USE_POP
  200.   if (!memcmp (inname, "po:", 3))
  201.     {
  202.       int status; char *user;
  203.  
  204.       for (user = &inname[strlen (inname) - 1]; user >= inname; user--)
  205.     if (*user == ':')
  206.       break;
  207.  
  208.       status = popmail (user, outname);
  209.       exit (status);
  210.     }
  211.  
  212.   setuid (getuid ());
  213. #endif /* MAIL_USE_POP */
  214.  
  215.   /* Check access to input file.  */
  216.   if (access (inname, R_OK | W_OK) != 0)
  217.     pfatal_with_name (inname);
  218.  
  219. #ifndef MAIL_USE_MMDF
  220. #ifndef MAIL_USE_SYSTEM_LOCK
  221.   /* Use a lock file named /usr/spool/mail/$USER.lock:
  222.      If it exists, the mail file is locked.  */
  223.   /* Note: this locking mechanism is *required* by the mailer
  224.      (on systems which use it) to prevent loss of mail.
  225.  
  226.      On systems that use a lock file, extracting the mail without locking
  227.      WILL occasionally cause loss of mail due to timing errors!
  228.  
  229.      So, if creation of the lock file fails
  230.      due to access permission on /usr/spool/mail,
  231.      you simply MUST change the permission
  232.      and/or make movemail a setgid program
  233.      so it can create lock files properly.
  234.  
  235.      You might also wish to verify that your system is one
  236.      which uses lock files for this purpose.  Some systems use other methods.
  237.  
  238.      If your system uses the `flock' system call for mail locking,
  239.      define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
  240.      and recompile movemail.  If the s- file for your system
  241.      should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
  242.      to bug-gnu-emacs@prep.ai.mit.edu so we can fix it.  */
  243.  
  244.   lockname = concat (inname, ".lock", "");
  245.   tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
  246.   strcpy (tempname, inname);
  247.   p = tempname + strlen (tempname);
  248.   while (p != tempname && p[-1] != '/')
  249.     p--;
  250.   *p = 0;
  251.   strcpy (p, "EXXXXXX");
  252.   mktemp (tempname);
  253.   unlink (tempname);
  254.  
  255.   while (1)
  256.     {
  257.       /* Create the lock file, but not under the lock file name.  */
  258.       /* Give up if cannot do that.  */
  259.       desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
  260.       if (desc < 0)
  261.         pfatal_with_name ("lock file--see source file lib-src/movemail.c");
  262.       close (desc);
  263.  
  264.       tem = link (tempname, lockname);
  265.       unlink (tempname);
  266.       if (tem >= 0)
  267.     break;
  268.       sleep (1);
  269.  
  270.       /* If lock file is a minute old, unlock it.  */
  271.       if (stat (lockname, &st) >= 0)
  272.     {
  273.       now = time (0);
  274.       if (st.st_ctime < now - 60)
  275.             unlink (lockname);
  276.     }
  277.     }
  278.  
  279.   delete_lockname = lockname;
  280. #endif /* not MAIL_USE_SYSTEM_LOCK */
  281. #endif /* not MAIL_USE_MMDF */
  282.  
  283.   if (fork () == 0)
  284.     {
  285.       setuid (getuid ());
  286.  
  287. #ifndef MAIL_USE_MMDF
  288. #ifdef MAIL_USE_SYSTEM_LOCK
  289.       indesc = open (inname, O_RDWR);
  290. #else  /* if not MAIL_USE_SYSTEM_LOCK */
  291.       indesc = open (inname, O_RDONLY);
  292. #endif /* not MAIL_USE_SYSTEM_LOCK */
  293. #else  /* MAIL_USE_MMDF */
  294.       indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
  295. #endif /* MAIL_USE_MMDF */
  296.  
  297.       if (indesc < 0)
  298.     pfatal_with_name (inname);
  299.  
  300. #if defined (BSD) || defined (XENIX)
  301.       /* In case movemail is setuid to root, make sure the user can
  302.      read the output file.  */
  303.       /* This is desirable for all systems
  304.      but I don't want to assume all have the umask system call */
  305.       umask (umask (0) & 0333);
  306. #endif /* BSD or Xenix */
  307.       outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
  308.       if (outdesc < 0)
  309.     pfatal_with_name (outname);
  310. #ifdef MAIL_USE_SYSTEM_LOCK
  311. #ifdef MAIL_USE_LOCKF
  312.       if (lockf (indesc, F_LOCK, 0) < 0) pfatal_with_name (inname);
  313. #else /* not MAIL_USE_LOCKF */
  314. #ifdef XENIX
  315.       if (locking (indesc, LK_RLCK, 0L) < 0) pfatal_with_name (inname);
  316. #else
  317.       if (flock (indesc, LOCK_EX) < 0) pfatal_with_name (inname);
  318. #endif
  319. #endif /* not MAIL_USE_LOCKF */
  320. #endif /* MAIL_USE_SYSTEM_LOCK */
  321.  
  322.       {
  323.     char buf[1024];
  324.  
  325.     while (1)
  326.       {
  327.         nread = read (indesc, buf, sizeof buf);
  328.         if (nread != write (outdesc, buf, nread))
  329.           {
  330.         int saved_errno = errno;
  331.         unlink (outname);
  332.         errno = saved_errno;
  333.         pfatal_with_name (outname);
  334.           }
  335.         if (nread < sizeof buf)
  336.           break;
  337.       }
  338.       }
  339.  
  340. #ifdef BSD
  341.       if (fsync (outdesc) < 0)
  342.     pfatal_and_delete (outname);
  343. #endif
  344.  
  345.       /* Check to make sure no errors before we zap the inbox.  */
  346.       if (close (outdesc) != 0)
  347.     pfatal_and_delete (outname);
  348.  
  349. #ifdef MAIL_USE_SYSTEM_LOCK
  350. #if defined (STRIDE) || defined (XENIX)
  351.       /* Stride, xenix have file locking, but no ftruncate.  This mess will do. */
  352.       close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
  353. #else
  354.       ftruncate (indesc, 0L);
  355. #endif /* STRIDE or XENIX */
  356. #endif /* MAIL_USE_SYSTEM_LOCK */
  357.  
  358. #ifdef MAIL_USE_MMDF
  359.       lk_close (indesc, 0, 0, 0);
  360. #else
  361.       close (indesc);
  362. #endif
  363.  
  364. #ifndef MAIL_USE_SYSTEM_LOCK
  365.       /* Delete the input file; if we can't, at least get rid of its
  366.      contents.  */
  367. #ifdef MAIL_UNLINK_SPOOL
  368.       /* This is generally bad to do, because it destroys the permissions
  369.      that were set on the file.  Better to just empty the file.  */
  370.       if (unlink (inname) < 0 && errno != ENOENT)
  371. #endif /* MAIL_UNLINK_SPOOL */
  372.     creat (inname, 0600);
  373. #endif /* not MAIL_USE_SYSTEM_LOCK */
  374.  
  375.       exit (0);
  376.     }
  377.  
  378.   wait (&status);
  379.   if (!WIFEXITED (status))
  380.     exit (1);
  381.   else if (WRETCODE (status) != 0)
  382.     exit (WRETCODE (status));
  383.  
  384. #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
  385.   unlink (lockname);
  386. #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
  387.   exit (0);
  388. }
  389.  
  390. /* Print error message and exit.  */
  391.  
  392. static void
  393. fatal (s1, s2)
  394.      char *s1, *s2;
  395. {
  396.   if (delete_lockname)
  397.     unlink (delete_lockname);
  398.   error (s1, s2, "");
  399.   exit (1);
  400. }
  401.  
  402. /* Print error message.  `s1' is printf control string, `s2' is arg for it. */
  403.  
  404. static void
  405. error (s1, s2, s3)
  406.      char *s1, *s2, *s3;
  407. {
  408.   printf ("movemail: ");
  409.   printf (s1, s2, s3);
  410.   printf ("\n");
  411. }
  412.  
  413. static void
  414. pfatal_with_name (name)
  415.      char *name;
  416. {
  417.   char *s;
  418.  
  419.   s = concat ("", strerror (errno), " for %s");
  420.   fatal (s, name);
  421. }
  422.  
  423. static void
  424. pfatal_and_delete (name)
  425.      char *name;
  426. {
  427.   char *s;
  428.  
  429.   s = concat ("", strerror (errno), " for %s");
  430.   unlink (name);
  431.   fatal (s, name);
  432. }
  433.  
  434. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  435.  
  436. static char *
  437. concat (s1, s2, s3)
  438.      char *s1, *s2, *s3;
  439. {
  440.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  441.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  442.  
  443.   strcpy (result, s1);
  444.   strcpy (result + len1, s2);
  445.   strcpy (result + len1 + len2, s3);
  446.   *(result + len1 + len2 + len3) = 0;
  447.  
  448.   return result;
  449. }
  450.  
  451. /* Like malloc but get fatal error if memory is exhausted.  */
  452.  
  453. static void *
  454. xmalloc (size)
  455.      unsigned int size;
  456. {
  457.   void *result = (void *) malloc (size);
  458.   if (!result)
  459.     fatal ("virtual memory exhausted", (char *) 0);
  460.   return result;
  461. }
  462.  
  463. /* This is the guts of the interface to the Post Office Protocol.  */
  464.  
  465. #ifdef MAIL_USE_POP
  466.  
  467. #include <sys/socket.h>
  468. #include <netinet/in.h>
  469. #include <netdb.h>
  470. #include <stdio.h>
  471. #include <pwd.h>
  472.  
  473. #ifdef USG
  474. #include <fcntl.h>
  475. /* Cancel substitutions made by config.h for Emacs.  */
  476. #undef open
  477. #undef read
  478. #undef write
  479. #undef close
  480. #endif /* USG */
  481.  
  482. #define NOTOK (-1)
  483. #define OK 0
  484. #define DONE 1
  485.  
  486. char *progname;
  487. FILE *sfi;
  488. FILE *sfo;
  489. char Errmsg[80];
  490.  
  491. static int debug = 0;
  492.  
  493. char *get_errmsg ();
  494. char *getenv ();
  495. int mbx_write ();
  496.  
  497. int
  498. popmail (user, outfile)
  499.      char *user;
  500.      char *outfile;
  501. {
  502.   char *host;
  503.   int nmsgs, nbytes;
  504.   char response[128];
  505.   register int i;
  506.   int mbfi;
  507.   FILE *mbf;
  508.   struct passwd *pw = (struct passwd *) getpwuid (getuid ());
  509.   if (pw == NULL)
  510.     fatal ("cannot determine user name");
  511.  
  512.   host = getenv ("MAILHOST");
  513.   if (host == NULL) 
  514.     {
  515.       fatal ("no MAILHOST defined");
  516.     }
  517.  
  518.   if (pop_init (host) == NOTOK) 
  519.     {
  520.       fatal (Errmsg);
  521.     }
  522.  
  523.   if (getline (response, sizeof response, sfi) != OK) 
  524.     {
  525.       fatal (response);
  526.     }
  527.  
  528.   if (pop_command ("USER %s", user) == NOTOK 
  529.       || pop_command ("RPOP %s", pw->pw_name) == NOTOK) 
  530.     {
  531.       pop_command ("QUIT");
  532.       fatal (Errmsg);
  533.     }
  534.  
  535.   if (pop_stat (&nmsgs, &nbytes) == NOTOK) 
  536.     {
  537.       pop_command ("QUIT");
  538.       fatal (Errmsg);
  539.     }
  540.  
  541.   if (!nmsgs)
  542.   {
  543.     pop_command ("QUIT");
  544.     return 0;
  545.   }
  546.  
  547.   mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
  548.   if (mbfi < 0)
  549.   {
  550.     pop_command ("QUIT");
  551.     pfatal_and_delete (outfile);
  552.   }
  553.   fchown (mbfi, getuid (), -1);
  554.  
  555.   if ((mbf = fdopen (mbfi, "w")) == NULL)
  556.     {
  557.       pop_command ("QUIT");
  558.       pfatal_and_delete (outfile);
  559.     }
  560.  
  561.   for (i = 1; i <= nmsgs; i++) 
  562.     {
  563.       mbx_delimit_begin (mbf);
  564.       if (pop_retr (i, mbx_write, mbf) != OK) 
  565.         {
  566.           pop_command ("QUIT");
  567.           close (mbfi);
  568.       unlink (outfile);
  569.       fatal (Errmsg);
  570.         }
  571.       mbx_delimit_end (mbf);
  572.       fflush (mbf);
  573.     }
  574.  
  575.   if (fsync (mbfi) < 0)
  576.     {
  577.       pop_command ("QUIT");
  578.       pfatal_and_delete (outfile);
  579.     }
  580.  
  581.   if (close (mbfi) == -1)
  582.     {
  583.       pop_command ("QUIT");
  584.       pfatal_and_delete (outfile);
  585.     }
  586.  
  587.   for (i = 1; i <= nmsgs; i++) 
  588.     {
  589.       if (pop_command ("DELE %d", i) == NOTOK) 
  590.         {
  591.       /* Better to ignore this failure.  */
  592.         }
  593.     }
  594.  
  595.   pop_command ("QUIT");
  596.   return (0);
  597. }
  598.  
  599. int
  600. pop_init (host)
  601.      char *host;
  602. {
  603.   register struct hostent *hp;
  604.   register struct servent *sp;
  605.   int lport = IPPORT_RESERVED - 1;
  606.   struct sockaddr_in sin;
  607.   register int s;
  608.  
  609.   hp = gethostbyname (host);
  610.   if (hp == NULL) 
  611.     {
  612.       sprintf (Errmsg, "MAILHOST unknown: %s", host);
  613.       return NOTOK;
  614.     }
  615.  
  616.   sp = getservbyname ("pop", "tcp");
  617.   if (sp == 0) 
  618.     {
  619.       strcpy (Errmsg, "tcp/pop: unknown service");
  620.       return NOTOK;
  621.     }
  622.  
  623.   sin.sin_family = hp->h_addrtype;
  624.   memcpy ((char *)&sin.sin_addr, hp->h_addr, hp->h_length);
  625.   sin.sin_port = sp->s_port;
  626.   s = rresvport (&lport);
  627.   if (s < 0) 
  628.     {
  629.       sprintf (Errmsg, "error creating socket: %s", get_errmsg ());
  630.       return NOTOK;
  631.     }
  632.  
  633.   if (connect (s, (char *)&sin, sizeof sin) < 0) 
  634.     {
  635.       sprintf (Errmsg, "error during connect: %s", get_errmsg ());
  636.       close (s);
  637.       return NOTOK;
  638.     }
  639.  
  640.   sfi = fdopen (s, "r");
  641.   sfo = fdopen (s, "w");
  642.   if (sfi == NULL || sfo == NULL) 
  643.     {
  644.       sprintf (Errmsg, "error in fdopen: %s", get_errmsg ());
  645.       close (s);
  646.       return NOTOK;
  647.     }
  648.  
  649.   return OK;
  650. }
  651.  
  652. int
  653. pop_command (fmt, a, b, c, d)
  654.      char *fmt;
  655. {
  656.   char buf[128];
  657.   char errmsg[64];
  658.  
  659.   sprintf (buf, fmt, a, b, c, d);
  660.  
  661.   if (debug) fprintf (stderr, "---> %s\n", buf);
  662.   if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK;
  663.  
  664.   if (getline (buf, sizeof buf, sfi) != OK) 
  665.     {
  666.       strcpy (Errmsg, buf);
  667.       return NOTOK;
  668.     }
  669.  
  670.   if (debug) fprintf (stderr, "<--- %s\n", buf);
  671.   if (*buf != '+') 
  672.     {
  673.       strcpy (Errmsg, buf);
  674.       return NOTOK;
  675.     } 
  676.   else 
  677.     {
  678.       return OK;
  679.     }
  680. }
  681.  
  682.     
  683. pop_stat (nmsgs, nbytes)
  684.      int *nmsgs, *nbytes;
  685. {
  686.   char buf[128];
  687.  
  688.   if (debug) fprintf (stderr, "---> STAT\n");
  689.   if (putline ("STAT", Errmsg, sfo) == NOTOK)
  690.     return NOTOK;
  691.  
  692.   if (getline (buf, sizeof buf, sfi) != OK) 
  693.     {
  694.       strcpy (Errmsg, buf);
  695.       return NOTOK;
  696.     }
  697.  
  698.   if (debug) fprintf (stderr, "<--- %s\n", buf);
  699.   if (*buf != '+') 
  700.     {
  701.       strcpy (Errmsg, buf);
  702.       return NOTOK;
  703.     } 
  704.   else 
  705.     {
  706.       sscanf (buf, "+OK %d %d", nmsgs, nbytes);
  707.       return OK;
  708.     }
  709. }
  710.  
  711. pop_retr (msgno, action, arg)
  712.      int (*action)();
  713. {
  714.   char buf[128];
  715.  
  716.   sprintf (buf, "RETR %d", msgno);
  717.   if (debug) fprintf (stderr, "%s\n", buf);
  718.   if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK;
  719.  
  720.   if (getline (buf, sizeof buf, sfi) != OK) 
  721.     {
  722.       strcpy (Errmsg, buf);
  723.       return NOTOK;
  724.     }
  725.  
  726.   while (1) 
  727.     {
  728.       switch (multiline (buf, sizeof buf, sfi)) 
  729.         {
  730.         case OK:
  731.           (*action)(buf, arg);
  732.           break;
  733.         case DONE:
  734.           return OK;
  735.         case NOTOK:
  736.           strcpy (Errmsg, buf);
  737.           return NOTOK;
  738.         }
  739.     }
  740. }
  741.  
  742. getline (buf, n, f)
  743.      char *buf;
  744.      register int n;
  745.      FILE *f;
  746. {
  747.   register char *p;
  748.   int c;
  749.  
  750.   p = buf;
  751.   while (--n > 0 && (c = fgetc (f)) != EOF)
  752.     if ((*p++ = c) == '\n') break;
  753.  
  754.   if (ferror (f)) 
  755.   {
  756.     strcpy (buf, "error on connection");
  757.     return NOTOK;
  758.   }
  759.  
  760.   if (c == EOF && p == buf) 
  761.   {
  762.     strcpy (buf, "connection closed by foreign host");
  763.     return DONE;
  764.   }
  765.  
  766.   *p = NULL;
  767.   if (*--p == '\n') *p = NULL;
  768.   if (*--p == '\r') *p = NULL;
  769.   return OK;
  770. }
  771.  
  772. multiline (buf, n, f)
  773.      char *buf;
  774.      register int n;
  775.      FILE *f;
  776. {
  777.   if (getline (buf, n, f) != OK) return NOTOK;
  778.   if (*buf == '.') 
  779.     {
  780.       if (*(buf+1) == NULL) 
  781.     return DONE;
  782.       else 
  783.         strcpy (buf, buf+1);
  784.     }
  785.   return OK;
  786. }
  787.  
  788. char *
  789. get_errmsg ()
  790. {
  791.   return strerror (errno);
  792. }
  793.  
  794. putline (buf, err, f)
  795.      char *buf;
  796.      char *err;
  797.      FILE *f;
  798. {
  799.   fprintf (f, "%s\r\n", buf);
  800.   fflush (f);
  801.   if (ferror (f)) 
  802.     {
  803.       strcpy (err, "lost connection");
  804.       return NOTOK;
  805.     }
  806.   return OK;
  807. }
  808.  
  809. mbx_write (line, mbf)
  810.      char *line;
  811.      FILE *mbf;
  812. {
  813.   fputs (line, mbf);
  814.   fputc (0x0a, mbf);
  815. }
  816.  
  817. mbx_delimit_begin (mbf)
  818.      FILE *mbf;
  819. {
  820.   fputs ("\f\n0, unseen,,\n", mbf);
  821. }
  822.  
  823. mbx_delimit_end (mbf)
  824.      FILE *mbf;
  825. {
  826.   putc ('\037', mbf);
  827. }
  828.  
  829. #endif /* MAIL_USE_POP */
  830.  
  831. #ifndef HAVE_STRERROR
  832. char *
  833. strerror (errnum)
  834.      int errnum;
  835. {
  836.   extern char *sys_errlist[];
  837.   extern int sys_nerr;
  838.  
  839.   if (errnum >= 0 && errnum < sys_nerr)
  840.     return sys_errlist[errnum];
  841.   return (char *) "Unknown error";
  842. }
  843.  
  844. #endif /* ! HAVE_STRERROR */
  845.